package com.thinking.project.monitorserver.controller;

import com.thinking.project.monitorserver.pojo.Worker;
import com.thinking.project.monitorserver.service.WorkerService;
import com.thinking.project.monitorserver.util.JwtTokenUtil;
import com.thinking.project.monitorserver.util.ResponseData;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @Description 功能描述：  登录判断业务入口
 * @Author tangsixiang@163.com
 * @DATE 2019/6/24 11:26
 * @Version 1.0
 **/

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    StringRedisTemplate template;
    @Autowired
    WorkerService workerService;
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
        }



    //处理登录
    @RequestMapping(value = "/userName", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseData login( @RequestParam("userName") String userName,
                              @RequestParam("password") String password) {
        ResponseData responseData = ResponseData.ok();

        Worker worker = new Worker();
        worker.setUsername(userName);
        worker.setPassword(password);
        Worker user = workerService.selectByUserName(worker);
        //先到数据库验证
        if (null == user) {
            responseData.putDataValue("reslutMes", "无对应账号，请核实账号!");
            responseData.putDataValue("reslutCode", 0);

        } else if (!user.getPassword().equalsIgnoreCase(worker.getPassword())) {
            responseData.putDataValue("reslutMes", "密码错误！!");
            responseData.putDataValue("reslutCode", -1);
        } else {
            //给用户jwt加密生成token
           String token = JwtTokenUtil.createToken(user,30);
            //封装成对象返回给客户端
            responseData.putDataValue("loginId", user.getWorkerId());
            responseData.putDataValue("token", token);
            responseData.putDataValue("user", user);
            template.opsForValue().set(String.format("user:login:mark:[%s]", user.getWorkerId()),String.valueOf(System.currentTimeMillis()), 30, TimeUnit.MINUTES);
        }
        return responseData;
    }

    /***
     * @Author tangsixiang
     * @Description   //退出登录 客户端自动消除token值
     * @Date 15:40 2019/6/24
     * @Param [map]
     ****/
    @RequestMapping(value = "/logout", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData logoutWorkder(@RequestHeader String Authorization) {
       Worker worker =JwtTokenUtil.verifyToken(Authorization,Worker.class);
        if(worker!=null){
            template.delete(String.format("user:login:mark:[%s]", worker.getWorkerId()));
        }
        return ResponseData.ok();
    }

}
